#include <iostream>
using namespace std;

char *getPi(int precision)
{
    long double pi = 0;
    int switcher = 1; // 正负号开关
    for (int i = 1; i <= 2147483647 / 10; i += 2)
    {
        pi += switcher * 1.0 / i;
        switcher = -switcher;
    }
    pi *= 4;

    char *resStr = new char[0],      // 结果字串
            *fmt = new char[0];      // 格式字串
    sprintf(fmt, "%s%d%s", "%.", precision, "Lf");
    sprintf(resStr, fmt, pi);
    return resStr;
}

int main()
{
    cout << endl
         << "学号：3170608043，姓名：石英钰" << endl
         << endl
         << "请输入一个精度, 以计算 pi 的值(1-9): ";
    int p;
    cin >> p;
    cout << "pi 的值为: " << getPi(p) << endl;
}
